package com.bear.cmcc.NetTestHTTP.asynTask;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.widget.Toast;

public class Tools {
	
	public static String getPwdE(String pwd, long applicationId) {
		String pwdE = "";
		try {
			byte[] source = pwd.getBytes("GBK");
			long a = 1255, b = 6173, m = 29282, x = applicationId;
			for (int i = 0; i < source.length; ++i) {
				byte e = (byte) (((a * x + b) % m) % 255);
				source[i] = (byte) (source[i] ^ e);
			}
			byte[] encryptedBytes = android.util.Base64.encode(source,
					android.util.Base64.NO_WRAP);
			pwdE = new String(encryptedBytes, "GBK");

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pwdE;
	}
	
	public static String encodeIMSI(String imsi) {
		StringBuffer hexString = new StringBuffer();
		MessageDigest algorithm;
		try {
			algorithm = MessageDigest.getInstance("MD5");
			algorithm.reset();			
			algorithm.update(imsi.getBytes("GBK"));
			byte[] messageDigest = algorithm.digest();
			//将IMSI的字节数据用MD5编码
			for(int i=0; i<messageDigest.length; i++){
				int val = 0xFF & (messageDigest[i]);
				if(val < 0x10){
					hexString.append("0");					
				}	
		     hexString.append(Integer.toHexString(val));
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hexString.toString().toUpperCase();
	}
	
	public static void showMessage(Context mContext, String text) {
		Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
	}

}
